package cn.edu.sdjzu.xg.eagle.controller;

import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.service.TeacherServiceImpl;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.ControllerHelper;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/TeacherController")
public class TeacherController extends HttpServlet {

    //多表查询
    @Override
    protected  void  doGet (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        JSONObject message = new JSONObject();

        try{
            String req_jsonStr = JSONUtil.getJSON(request);
            JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);

            if (req_jsonObj == null) {
                message.put("message", "未收到请求消息!");
                response.getWriter().println(message);
                return;
            }

            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<Teacher> teacherList = TeacherServiceImpl.getInstance().findAll(conditionList);
            String teacher_json = JSON.toJSONString(teacherList,
                    JSONWriter.Feature.WriteMapNullValue,
                    JSONWriter.Feature.WriteNullListAsEmpty);

            response.getWriter().println(teacher_json);
        }  catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (BysjException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            message.put("message", "服务器错误: " + e.getClass().getSimpleName() + " - " + e.getMessage());
            response.getWriter().println(message);
        }
    }

//    {
//        "pagination":   {
//        "pageNo":2,
//                "pageSize":2
//    },
//        "conditionList": [
//        {
//            "value": "张",
//            "key": "name",
//            "operator": "LIKE"
//        }
//      ]
//    }
    //查询，分页
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        JSONObject message = new JSONObject();

        String req_jsonStr = JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null) {
            message.put("message", "未收到请求消息!");
            response.getWriter().println(message);
            return;
        }

        try{
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<Teacher> teacherList = TeacherServiceImpl.getInstance().findAll(conditionList,pagination);
            message.put("totalNum",pagination.getTotalNum());
            message.put("records",teacherList);

            response.getWriter().println(message);
        }  catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (BysjException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            message.put("message", "服务器错误: " + e.getClass().getSimpleName() + " - " + e.getMessage());
            response.getWriter().println(message);
        }
    }
}
